package com.niannian.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.BaseActivity;
import com.niannian.R;

/**
 * 消息声音提醒设置
 * 
 * @author shenglin
 * 
 */
public class NewsSoundSettingActivity extends BaseActivity {
	LinearLayout rl_notification_sound_setting, rl_notification_vibrate_setting;
	ImageView iv_sound_setting, iv_vibrate_setting, iv_close_news_notification;
	private SharedPreferences sp;
	Boolean notification = true;
	Boolean isSoundOpen = true;
	Boolean isVibrate = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_sound_setting);
		sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		initTopView();
		initView();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("消息提醒");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Intent i =
				// getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
				// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// startActivity(i);
				myfinish();
			}
		});
	}

	private void initView() {
		rl_notification_sound_setting = (LinearLayout) findViewById(R.id.rl_notification_sound_setting);
		iv_sound_setting = (ImageView) findViewById(R.id.iv_sound_setting);
		rl_notification_vibrate_setting = (LinearLayout) findViewById(R.id.rl_notification_vibrate_setting);
		iv_vibrate_setting = (ImageView) findViewById(R.id.iv_vibrate_setting);
		iv_close_news_notification = (ImageView) findViewById(R.id.iv_close_news_notification);
		notification = sp.getBoolean("notificationsetting", true);
		if (notification) {
			iv_close_news_notification.setImageResource(R.drawable.sound_open);
			rl_notification_sound_setting.setVisibility(View.VISIBLE);
			rl_notification_vibrate_setting.setVisibility(View.VISIBLE);
		} else {
			iv_close_news_notification.setImageResource(R.drawable.sound_close);
			rl_notification_sound_setting.setVisibility(View.GONE);
			rl_notification_vibrate_setting.setVisibility(View.GONE);
		}

		isSoundOpen = sp.getBoolean("soundsetting", true);
		if (isSoundOpen) {
			iv_sound_setting.setImageResource(R.drawable.sound_open);
		} else {
			iv_sound_setting.setImageResource(R.drawable.sound_close);
		}
		isVibrate = sp.getBoolean("isvibrate", true);
		if (isVibrate) {
			iv_vibrate_setting.setImageResource(R.drawable.sound_open);
		} else {
			iv_vibrate_setting.setImageResource(R.drawable.sound_close);
		}

		iv_close_news_notification.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (notification) {
					iv_close_news_notification.setImageResource(R.drawable.sound_close);
					notification = false;
					rl_notification_sound_setting.setVisibility(View.GONE);
					rl_notification_vibrate_setting.setVisibility(View.GONE);
				} else {
					iv_close_news_notification.setImageResource(R.drawable.sound_open);
					notification = true;
					rl_notification_sound_setting.setVisibility(View.VISIBLE);
					rl_notification_vibrate_setting.setVisibility(View.VISIBLE);
				}

				Editor edit = sp.edit();
				edit.putBoolean("notificationsetting", notification);
				edit.commit();
			}
		});

		iv_sound_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isSoundOpen) {
					iv_sound_setting.setImageResource(R.drawable.sound_close);
					isSoundOpen = false;
				} else {
					iv_sound_setting.setImageResource(R.drawable.sound_open);
					isSoundOpen = true;
				}

				Editor edit = sp.edit();
				edit.putBoolean("soundsetting", isSoundOpen);
				edit.commit();
			}
		});

		iv_vibrate_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isVibrate) {
					iv_vibrate_setting.setImageResource(R.drawable.sound_close);
					isVibrate = false;
				} else {
					iv_vibrate_setting.setImageResource(R.drawable.sound_open);
					isVibrate = true;
				}

				Editor edit = sp.edit();
				edit.putBoolean("isvibrate", isVibrate);
				edit.commit();
			}
		});

	}
}
